<?php
/**
 * 银行卡相关
 * User: wuhao
 * Date: 2017/5/5
 * Time: 9:22
 */
namespace app\backend\controller;

use app\backend\model\Bank;
use app\backend\model\Member;
use app\backend\model\Withdraw;
use Business\sms\VComSms;
use think\Controller;

class BankController extends Controller
{
    private $_memberId = null;
    private $_dtype    = null;
    private $_mobile   = null;
    private $_nickname = null;
    public function __construct()
    {
        parent::__construct();
        $memberInfo = $this->getLoginInfo();
        $this->_memberId = $memberInfo['mid'];
        $this->_dtype    = $memberInfo['dtype'];
        $this->_mobile   = $memberInfo['mobile'];
        $this->_nickname = $memberInfo['nickname'];
    }

    /**
     * 获取银行代码
     * @date   2016-01-10
     * @author Lanlc
     *
     * @return
     */
    public function bankCode()
    {
        $bankModel = new Bank();
        $res = $bankModel->bankCode();
        $this->apiReturn(200, ['list' => $res]);
    }


    /**
     * 获取支行信息
     * @author dwer
     * @date   2016-07-08
     *
     * @return
     */
    public function subbranchList()
    {
        $page = I('post.page', 1);
        $size = I('post.size', 50);
        $name = I('post.name', '');

        $cityId = I('post.city_id', 0);
        $bankId = I('post.bank_id', 0);

        if (!$cityId || !$bankId) {
            $this->apiReturn(400, [], '参数错误');
        }

        $bankModel = new Bank();
        $res = $bankModel->getSubbranch($cityId, $bankId, $name, $page, $size);

        if ($res === false) {
            $this->apiReturn(500, [], '系统错误');
        } else {
            $this->apiReturn(200, ['list' => $res], '');
        }
    }

    /**
     * 获取银行省信息
     * @date   2016-01-10
     * @author Lanlc
     *
     *
     * @return
     */
    public function bankProvince()
    {
        $bankModel = new Bank();
        $res = $bankModel->getBankProvince();
        $this->apiReturn(200, ['list' => $res]);
    }

    /**
     * 获取银行市信息
     * @date   2016-01-10
     * @author Lanlc
     *
     * @param  int  $provinceId  省ID
     *
     * @return
     */
    public function bankArea()
    {
        $provinceId = I('post.provinceId', '', 'intval');
        if (!$provinceId) {
            $this->apiReturn(400, [], '参数错误');
        }
        $bankModel = new Bank();
        $res = $bankModel->getBankArea($provinceId);
        $this->apiReturn(200, ['list' => $res]);
    }

    /**
     * 绑定银行卡
     * @date   2017-05-05
     * @author wuhao
     *
     * @return
     */
    public function addBankCard()
    {
        $cardType = I('post.card_type', 0, 'intval');//卡类型 0=借记卡
        $username = I('post.username', '', 'strval');//开卡人姓名
        $bankCode = I('post.bank_code', '', 'strval');//银行代码
        $bankCard = I('post.bank_card', '', 'strval');//银行卡号
        $vcode    = I('post.vcode', '', 'strval');    //手机验证码
        if (!in_array($cardType, [0]) || !$username || !$bankCode || !$bankCard|| !$vcode) {
            $this->apiReturn(400, [], '参数有误');
        }
        if ($username != $this->_nickname) {
            //$this->apiReturn(400, [], '当前用户名与开卡人姓名不一致');
        }
        //检查验证码
        $sms = new VComSms();
        $check = $sms->ChkVerifyCode($this->_mobile, 'bank_card', $vcode);
        if (!$check) {
            $this->apiReturn(403, [], '手机验证码错误');
        } elseif ($check === -1) {
            $this->apiReturn(403, [], '验证码已经失效');
        }

        //判断是否已经添加过银行卡了
        $bankModel = new Bank();
        $cardInfo = $bankModel->getBankCardByMid($this->_memberId);
        if ($cardInfo) {
            $this->apiReturn(402, [], '银行卡已经添加过了');
        }

        //添加银行卡
        $data = [
            'mid'         => $this->_memberId,
            'card_type'   => $cardType,
            'username'    => $username,
            'bank_code'   => $bankCode,
            'bank'        => $bankCard,
            'status'      => 1,
            'create_time' => time(),
            'update_time' => time(),
        ];
        $res = $bankModel->addBankCard($data);
        if ($res) {
            $this->apiReturn(200, [], '添加成功');
        } else {
            zft_log('bank/add', $bankModel->getLastSql());
            $this->apiReturn(201, [], '添加出错');
        }
    }

    /**
     * 删除绑定的银行卡
     * @date   2017-05-05
     * @author wuhao
     *
     * @return
     */
    public function deleteBankCard()
    {
        $bankCard = I('post.bank_card', '', 'strval');//银行卡号
        $vcode    = I('post.vcode', '', 'strval');    //手机验证码
        if (!$bankCard || !$vcode) {
            $this->apiReturn(400, [], '参数有误');
        }
        //检查验证码
        $sms = new VComSms();
        $check = $sms->ChkVerifyCode($this->_mobile, 'bank_card_del', $vcode);
        if (!$check) {
            $this->apiReturn(403, [], '手机验证码错误');
        } elseif ($check === -1) {
            $this->apiReturn(403, [], '验证码已经失效');
        }
        $bankModel = new Bank();
        $cardInfo  = $bankModel->getBankCard($bankCard);
        if (!$cardInfo) {
            $this->apiReturn(401, [], '找不到对应的银行卡信息');
        }
        if ($cardInfo['mid'] != $this->_memberId) {
            $this->apiReturn(402, [], '非绑定人不能操作');
        }
        if ($cardInfo['status'] == 0) {
            $this->apiReturn(404, [], '银行卡已经删除，请勿重复操作');
        }
        //判断是否有提现申请到这个账户
        $wdModel = new Withdraw();
        $wdList = $wdModel->getListByAccount($bankCard, 0, 'id');
        if ($wdList) {
            $this->apiReturn(405, [], '存在未完成的提现申请，不能删除');
        }

        //删除银行卡
        $res = $bankModel->deleteBankCard($bankCard, $this->_memberId);
        if ($res) {
            $this->apiReturn(200, [], '删除成功');
        } else {
            zft_log('bank/delete', $bankModel->getLastSql());
            $this->apiReturn(200, [], '删除出错');
        }
    }
}